/**
* Copyright 2012 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.dc.client.presenter;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.nabla.dc.client.ui.LoginWindowUi;
import com.nabla.wapp.client.general.Application;
import com.nabla.wapp.client.general.LoggerFactory;
import com.nabla.wapp.client.mvp.AbstractCanvasPresenter;
import com.nabla.wapp.client.mvp.ICanvasDisplay;
import com.nabla.wapp.client.ui.ILoginUi;
import com.nabla.wapp.shared.auth.User;
import com.nabla.wapp.shared.general.CommonServerErrors;
import com.nabla.wapp.shared.slot.ISlot;


public class LoginWindow extends AbstractCanvasPresenter<LoginWindow.IDisplay> {

	public interface IDisplay extends ICanvasDisplay, ILoginUi {}

	private static final Logger		log = LoggerFactory.getLog(LoginWindow.class);
	private final ISlot				onSuccess;

	public LoginWindow(final ISlot onSuccess) {
		super(new LoginWindowUi());
		this.onSuccess = onSuccess;
	}

	@Override
	public void bind() {
		super.bind();
		registerHandler(getDisplay().getSubmitSlots().connect(onLoginClick));
	}

	private ISlot onLoginClick = new ISlot() {
        @Override
		public void invoke() {
		    final IDisplay d = getDisplay();
		    Application.getInstance().getUserSessionManager().createSession(d.getOrganisation(), d.getUserName(), d.getPassword(), d.getKeepLoggedIn(), new AsyncCallback<User>() {

			    @Override
			    public void onFailure(final Throwable caught) {
				    log.log(Level.WARNING, "login failed", caught);
				    d.showError(CommonServerErrors.INVALID_USER_PASSWORD);
				    d.setSaving(false);
			    }

			    @Override
			    public void onSuccess(@SuppressWarnings("unused") final User result) {
				    onSuccess.invoke();
			    }
		    });
		}
	};

}
